package com.cloudxingkong.dianyinke.NewTeacher.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Ui.ImagePagerActivity;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.DataNumFormat;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.NoScrollGridView;
import com.cloudxingkong.dianyinke.NewTeacher.bean.TeacherDT;
import com.cloudxingkong.dianyinke.R;

import java.util.List;

/**
 * Created by 云星空科技 on 2016/3/30.
 */
public class DTLVAdapter extends BaseAdapter{
    private List<TeacherDT.ListBean> list;
    private Context context;
    private getOnTouchDTPosition dtPosition;

    public DTLVAdapter(List<TeacherDT.ListBean> list, Context context, getOnTouchDTPosition dtPosition) {
        this.list = list;
        this.dtPosition=dtPosition;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView=View.inflate(context, R.layout.item_dt_listview,null);
            holder=new ViewHolder();
            holder.tvDay= (TextView) convertView.findViewById(R.id.item_dt_tvday);
            holder.tvContent= (TextView) convertView.findViewById(R.id.item_dt_content);
            holder.gridView= (NoScrollGridView) convertView.findViewById(R.id.dt_gridview);
            holder.tvMonth= (TextView) convertView.findViewById(R.id.item_dt_tvmonth);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        dtPosition.getPosition(position);
        holder.tvContent.setText(list.get(position).getText());

        /**
         * 填充时间
         */
        String serverTime = list.get(position).getTs();   //获取时间
        String ts = serverTime.substring(8, 10);      //截取
        holder.tvDay.setText(ts);     //给day添加数据
        /*long l = System.currentTimeMillis();
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=new Date(l);*/
        //Log.e("test","现在时间 "+f.format(date).substring(0,f.format(date).indexOf(" ")).equals(serverTime.substring(0,serverTime.indexOf(" "))));
        String month = DataNumFormat.formatStr(serverTime);
        String setMonth = month.substring(month.indexOf("年") + 1, month.indexOf("月"));
        holder.tvMonth.setText(setMonth+"月");

        /**
         * 填充图片
         */
        int id = list.get(position).getId();
        holder.gridView.setTag(id);
        boolean b = list.get(position).getPics().isEmpty();
        if (b==false){
            if (holder.gridView.getTag()!=null&&holder.gridView.getTag().equals(id)){

                final String pics = list.get(position).getPics();
                final String[] split = pics.split(",");
                if (!pics.equals("")){
                    if (pics.indexOf(",")==-1) {
                        split[0]=pics;
                        holder.gridView.setVisibility(View.VISIBLE);
                        holder.gridView.setAdapter(new DTGVAdapter(split, context));
                        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                imageBrower(position, split);
                            }
                        });
                    }else{
                        holder.gridView.setVisibility(View.VISIBLE);
                        holder.gridView.setAdapter(new DTGVAdapter(split, context));
                        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                imageBrower(position, split);
                            }
                        });
                    }
                }else{
                    holder.gridView.setVisibility(View.GONE);
                }
            }
        }else{
            holder.gridView.setVisibility(View.GONE);
        }

        return convertView;
    }

    private void imageBrower(int position, String[] pics) {
        Intent intent = new Intent(context, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, pics);
        context.startActivity(intent);
    }

    private class ViewHolder{
        NoScrollGridView gridView;
        TextView tvDay,tvContent,tvMonth;
    }

    public interface getOnTouchDTPosition{
        void getPosition(int position);
    }
}
